package com.gzhryc.net_marketing.supplier.pay.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.supplier.pay.dao.SupplierBusinessOrderLogDao;
import com.gzhryc.net_marketing.supplier.pay.dao.db.SupplierBusinessOrderLog;
import com.gzhryc.net_marketing.supplier.pay.dao.enums.SupplierBusinessOrderLogEnum;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class SupplierBusinessOrderLogService {

    static Logger log = Logger.getLogger(SupplierBusinessOrderLogService.class);

    public static SupplierBusinessOrderLogService self() {
        return MultiDBTools.getService(SupplierBusinessOrderLogService.class, true);
    }

    public static SupplierBusinessOrderLogService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, SupplierBusinessOrderLogService.class, true);
    }

    SupplierBusinessOrderLogDao dao;

    public SupplierBusinessOrderLogService(String jdbcKey) {
        dao = new SupplierBusinessOrderLogDao(jdbcKey);
    }

    public List<SupplierBusinessOrderLog> findByTradeNo(String tradeNo){
        if(StringTools.isNotBlank(tradeNo)){
            Conditions conditions = new Conditions(SupplierBusinessOrderLog.class);
            conditions.and().is("tradeNo",tradeNo);
            conditions.asc("createDate");
            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(SupplierBusinessOrderLog businessOrderLog){
        businessOrderLog.setCreateDate(new Date());
        try {
            return dao.insert(businessOrderLog) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean addCreateOrder(String tradeNo,String programType,Integer operatorType,String operator){
        SupplierBusinessOrderLog orderLog = new SupplierBusinessOrderLog();
        orderLog.setTradeNo(tradeNo);
        orderLog.setProgramType(programType);
        orderLog.setOperateContent("创建订单");
        orderLog.setOperatorType(operatorType);
        orderLog.setOperator(operator);
        orderLog.setOperateState(SupplierBusinessOrderLogEnum.OperateState.Success.index());
        orderLog.setCreateDate(new Date());
        try {
            return dao.insert(orderLog) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean addSuccess(String tradeNo,String operateContent,String programType,Integer operatorType,String operator){
        SupplierBusinessOrderLog orderLog = new SupplierBusinessOrderLog();
        orderLog.setTradeNo(tradeNo);
        orderLog.setProgramType(programType);
        orderLog.setOperateContent(operateContent);
        orderLog.setOperatorType(operatorType);
        orderLog.setOperator(operator);
        orderLog.setOperateState(SupplierBusinessOrderLogEnum.OperateState.Success.index());
        orderLog.setCreateDate(new Date());
        try {
            return dao.insert(orderLog) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean addError(String tradeNo,String operateContent,String operateError,String programType,Integer operatorType,String operator){
        SupplierBusinessOrderLog orderLog = new SupplierBusinessOrderLog();
        orderLog.setTradeNo(tradeNo);
        orderLog.setProgramType(programType);
        orderLog.setOperateContent(operateContent);
        orderLog.setOperatorType(operatorType);
        orderLog.setOperator(operator);
        orderLog.setOperateState(SupplierBusinessOrderLogEnum.OperateState.Error.index());
        orderLog.setOperateError(operateError);
        orderLog.setCreateDate(new Date());
        try {
            return dao.insert(orderLog) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
